package SMSGUI;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.*;
import javax.swing.text.html.*;

public class classifyData {
	final static int interval = 1000;
	static int i;
	static JLabel label;
	static JProgressBar pb;
	static Timer timer;
	static JButton button;
	static HashMap<Integer, String> tweetMap;
	static JPanel panel1 = new JPanel();
	

	public static void classifingData(final HashMap<Integer, String[]> excelData) {
		int number;
		String [] data;
		String [] nameS = new String[2];
		String tweet;
		tweetMap = new HashMap<Integer, String>();
		Iterator<Integer> kExcelData = excelData.keySet().iterator();
		while (kExcelData.hasNext()){
			nameS = new String[2];
			number = kExcelData.next();
			data = excelData.get(number);
			tweet = data[0];
			tweetMap.put(number, tweet);	
		}

		final JFrame frame = new JFrame("Bezig...");
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension scrnsize = toolkit.getScreenSize();
		frame.setBounds((int) ((float) .3 * scrnsize.width),
			    (int) ((float) 0.3 * scrnsize.height), 550, 450);		button = new JButton("Start");
		button.addActionListener(new  ActionListener() {
			public void actionPerformed(ActionEvent evt){
				button.setEnabled(false);
				i = 0;
				String str = "<html>" + "<font color=\"#008000\">" + "<b>"
						+ "Klaar met klassificeren" + "</b>"
						+ "</font>" + "</html>";
				label.setText(str);
		
				
				HashMap <Integer, String> tweets = new HashMap<Integer, String>();
				
				//correct tweets
				str = "<html>" + "<font color=\"#FF0800\">" + "<b>"
						+ "Bezig met corrigeren" + "</b>"
						+ "</font>" + "</html>";
				label.setText(str);
				Iterator<Integer> k = tweetMap.keySet().iterator();
				int index;
				String data1;
				String tweet;
				while(k.hasNext()){
					index = k.next();
					//System.out.println(done+"\t"+(double)(done/amountOftweets)*100);
					updatePb();
					data1 = tweetMap.get(index);
					tweet = data1;
					//tweet = Functions.CorrectTweet(tweet);			
					tweets.put( index, tweet );
				}
				//end correct tweets
				pb.setValue(0);
				i=0;
				str = "<html>" + "<font color=\"#FF0000\">" + "<b>"
						+ "Bezig met klasificeren" + "</b>"
						+ "</font>" + "</html>";
				
				label.setText(str);
				panel1.update(panel1.getGraphics());
				label.update(label.getGraphics());
				HashMap<Integer, Integer> sentimentMap;
				HashMap<Integer, String[]> sentimentDing = new HashMap<Integer, String[]>();
				String sent;
				
				try {
					sentimentMap = Functions.classify(tweets);
					Iterator<Integer> indexes = (Iterator<Integer>) sentimentMap.keySet().iterator();
					while(indexes.hasNext())
					{
						String[] senti = new String[2];
						int index2 = indexes.next();
						System.out.println(index2 + " " + sentimentMap.get(index2));
						sent = "" + sentimentMap.get(index2);
						tweet = excelData.get(index2)[0];
						senti[0]=tweet;
						senti[1]=sent;
						System.out.println(senti[1] + " " + senti[0]);
						sentimentDing.put(index2, senti);
						
					}
					Iterator<Integer> tweetlistIterator = excelData.keySet().iterator();
					int cur;
					String[] data;
					
					while (tweetlistIterator.hasNext()){
						updatePb();
						cur = tweetlistIterator.next();
						data = excelData.get(cur);
						if(sentimentMap.containsKey(cur))
							data[1] = "" + sentimentMap.get(cur);
						else
							data[1] = "" + 0;
						//System.out.println(data[0]+"\n"+data[1]);
						excelData.put(cur,data);
					}
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					str = "<html>" + "<font color=\"#FF0000\">" + "<b>"
							+ "Fout bij het klasificeren" + "</b>"
							+ "</font>" + "</html>";
					label.setText(str);
					e2.printStackTrace();
				}//end classify
				int number;
				String [] data;
				String [] nameS = new String[2];
				Iterator<Integer> kExcelData = excelData.keySet().iterator();
				while (kExcelData.hasNext()){
					number = kExcelData.next();
					data = excelData.get(number);
					tweet = data[0];
					tweetMap.put(number, tweet);				
				}
				//start names
				
				kExcelData = excelData.keySet().iterator();
				HashMap<Integer, String[]> nameAndSentiment = new HashMap<Integer, String[]>();
				while (kExcelData.hasNext()){
					nameS = new String[2];
					number = kExcelData.next();
					data = excelData.get(number);
					nameS[0] = data[2];
					nameS[1] = data[1];
					nameAndSentiment.put(number,nameS);
				}
				
				try {
					float[] sentimentList = Functions.genderFinder(nameAndSentiment);
					Dashboard.makePanels(sentimentList);
				} catch (IOException e1) {

				}//end names
				
				Iterator<Integer> ding = sentimentDing.keySet().iterator();
				
				int count;
				String [][] tweetSentiment = new String[sentimentDing.keySet().size()][3];
				String[] extract;
				int counter = 0;
				while(ding.hasNext()){
					
					count = ding.next();
					extract = sentimentDing.get(count);
					tweetSentiment[counter][0] = ""+count;
					tweetSentiment[counter][1]=extract[0];
					tweetSentiment[counter][2]=extract[1];
					
					counter++;
					
				}
				
				
				ScrollableJTable.table(tweetSentiment, excelData);
				
				
				
				

			}});
			

		pb = new JProgressBar(0, tweetMap.size());
		pb.setValue(0);
		pb.setStringPainted(true);
		label = new JLabel("<html>" + "<font color=\"#FF0800\">" + "<b>"
				+ "Klik start" + "</b>"
				+ "</font>" + "</html>");
		
		JPanel panel = new JPanel();
		panel.add(button);
		panel.add(pb);

		
		panel1.setLayout(new BorderLayout());
		panel1.add(panel, BorderLayout.NORTH);
		panel1.add(label, BorderLayout.CENTER);
		panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		frame.setContentPane(panel1);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
		// Create a timer.
		public static void updatePb(){
			i++;
			
			pb.setValue(i);
			pb.update(pb.getGraphics());
			
		}

	
		

	}


